package com.example.bootintegration.service;

import lombok.extern.log4j.Log4j2;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Quintoy
 * @date 2024/11/28
 */
@Log4j2
@Component
public class RedisListener implements MessageListener {

    public static Map<String, DeferredResult<String>> map = new ConcurrentHashMap<>();


    public void addDeferredResult(String key, DeferredResult<String> deferredResult) {
        log.info("start wait....");
        map.put(key, deferredResult);

    }

    // 每一个都是新线程
    @Override
    public void onMessage(Message message, byte[] pattern) {
        log.info("从消息通道={}监听到消息",new String(pattern));

        log.info("从消息通道={}监听到消息",new String(message.getChannel()));

        log.info("元消息={}",new String(message.getBody()));
        DeferredResult<String> remove = map.remove(new String(message.getBody()));
        if (remove != null)
            remove.setResult("success: orderNo");
        log.debug("消费结束");

    }
}
